### DIVIDE GENES INTO GENE FAMILIES part 2
import pandas as pd
import markov_clustering as mc
import networkx as nx
import numpy as np

data=pd.read_csv('result_nangua_filtered',sep='\t')
G=nx.Graph()
edges_with_weights=[(data['qgene'][i],data['sgene'][i],data["bitscore"][i]) for i in range(len(data))]
G.add_weighted_edges_from(edges_with_weights)
matrix=nx.to_scipy_sparse_matrix(G)
# matrix = csr_matrix(nx.adjacency_matrix(G)) (for networkx version > 2.5)

result11=mc.run_mcl(matrix,inflation=1.1)
result15=mc.run_mcl(matrix,inflation=1.5)
result20=mc.run_mcl(matrix,inflation=2)

clusters11=mc.get_clusters(result11)
clusters15=mc.get_clusters(result15)
clusters20=mc.get_clusters(result20)

nodes=list(G.nodes())

gene_families11=np.array([[nodes[element] for element in tup] for tup in clusters11])
gene_families15=np.array([[nodes[element] for element in tup] for tup in clusters15])
gene_families20=np.array([[nodes[element] for element in tup] for tup in clusters20])
gene_families11 = np.empty(len(clusters11), dtype=object) 





np.save(file='gene_families11',arr=gene_families11)
np.save(file='gene_families15',arr=gene_families15)
np.save(file='gene_families20',arr=gene_families20)

# The file "gene_families11.npy" is chosen for downstream analysis
# Result can be later flatterned by [item for items in gene_families for item in items]